/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.me.mjw.rememome.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import org.me.mjw.rememome.R;
import org.me.mjw.rememome.db.ReMemoMeDbAdapter;
import org.me.mjw.rememome.memo.Memo;

/**
 *
 * @author MWaldman
 */
public class ListFrameActivity extends Activity
{

    @Override
    protected void onResume()
    {
        super.onResume();
        ListView lv;

        lv = (ListView) findViewById(R.id.list_items);
        refreshList(lv);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        final ListView lv;
        final RadioGroup rg;

        setContentView(R.layout.list_frame);
        lv = (ListView) findViewById(R.id.list_items);
        lv.setTextFilterEnabled(true);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {

            public void onItemClick(AdapterView<?> av, View v, int position, long id)
            {
//                String item = (String) getListView().getItemAtPosition(position);
                String item = (String) lv.getItemAtPosition(position);
                Toast.makeText(getApplicationContext(), "Set Properties for: " + item,
                        Toast.LENGTH_LONG).show();
            }
        });
        lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener()
        {

            public boolean onItemLongClick(AdapterView<?> av, View v, int position, long id)
            {
                deleteWithWarning(position, lv);
                return true;
            }
        });
        rg = (RadioGroup) findViewById(R.id.sort_list_group);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
        {

            public void onCheckedChanged(RadioGroup arg0, int arg1)
            {
                refreshList(lv);
            }
        });

        refreshList(lv);
    }

    public void resortList(View v)
    {
        ListView lv;

        lv = (ListView) findViewById(R.id.list_items);
        refreshList(lv);
    }

    private void refreshList(ListView lv)
    {
        ListAdapter listAdapter;
        String[] list;
        ReMemoMeDbAdapter.OrderType type;
        ReMemoMeDbAdapter dbAdapter;
        RadioGroup rg;
        String newSort;
        String oldSort;
        String alphaSort;
        String checked;
        int checkedId;
        RadioButton rb;

        rg = (RadioGroup) findViewById(R.id.sort_list_group);
        newSort = getString(R.string.newest_first_order_type);
        oldSort = getString(R.string.oldest_first_order_type);
        alphaSort = getString(R.string.alpha_order_type);
        checkedId = rg.getCheckedRadioButtonId();
        rb = (RadioButton) findViewById(checkedId);
        checked = (String) rb.getText();

        if (newSort.equals(checked))
        {
            type = ReMemoMeDbAdapter.OrderType.NEWEST;
        }
        else if (oldSort.equals(checked))
        {
            type = ReMemoMeDbAdapter.OrderType.OLDEST;
        }
        else if (alphaSort.equals(checked))
        {
            type = ReMemoMeDbAdapter.OrderType.ALPHA;
        }
        else
        {
            type = null;
        }

        dbAdapter = new ReMemoMeDbAdapter(this);
        dbAdapter.open();

        list = getList(type, dbAdapter);
        listAdapter = new ArrayAdapter<String>(this, R.layout.list_item, list);
        lv.setAdapter(listAdapter);
        dbAdapter.close();
    }
    // Look up List in database based on type

    private String[] getList(ReMemoMeDbAdapter.OrderType type, ReMemoMeDbAdapter dbAdapter)
    {
        String[] list;
        List<String> listObj;
        List<Memo> listMemo;

        listObj = new ArrayList<String>();
//
        listMemo = dbAdapter.listMemos(type);

        for (Memo m : listMemo)
        {
            listObj.add(m.getMemo());
        }

        list = listObj.toArray(new String[0]);

        return list;
    }

    private void deleteWithWarning(int position, ListView lv)
    {
        final String item;
        AlertDialog deleteDialog;
        final ListView lvf = lv;
//        item = (String) getListView().getItemAtPosition(position);
        item = (String) lv.getItemAtPosition(position);

        deleteDialog = new AlertDialog.Builder(this).create();
        deleteDialog.setTitle("Delete Item");
        deleteDialog.setMessage("Are you sure you want to delete: " + item);
        deleteDialog.setButton("Yes", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                deleteItem(item);
                refreshList(lvf);
            }
        });
        deleteDialog.setButton2("Cancel", new OnClickListener()
        {

            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // Do Nothing
            }
        });
        deleteDialog.show();

    }

    private void deleteItem(String item)
    {
        ReMemoMeDbAdapter dbAdapter;
        dbAdapter = new ReMemoMeDbAdapter(this);
        dbAdapter.open();

        Toast.makeText(this, "Deleted: " + item, Toast.LENGTH_SHORT).show();

        dbAdapter.deleteMemo(item);

        dbAdapter.close();
    }
}
